﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FB.Data.Models;
using FB.Providers.EmployeeInfo;

namespace FB.Extensions
{
    public static class EmployeeExtensions
    {
        public static EmployeeInfoLite ToEmployeeInfoLite(this Employee employee)
        {
            var result = new EmployeeInfoLite
            {
                EMPCode = employee.EMPCode,
                FirstName = employee.FirstName,
                LastName = employee.LastName,
                Alias = employee.Alias,
                ChineseName = employee.ChineseName,
                Email = employee.Email,
                Status = employee.Status,
                UserID = employee.UserID,
            };

            return result;
        }


        public static EmployeeInfo ToEmployeeInfo(this Employee employee)
        {
            if (null == employee) return null;

            var result = ConvertTo<EmployeeInfo>(employee);

            if (null != employee.Subordinates && employee.Subordinates.Count > 0)
                result.Subordinates = employee.Subordinates.ToList().ConvertAll(c => c.ToEmployeeInfoLite());

            if (null != employee.Supervisors && employee.Supervisors.Count > 0)
                result.Supervisors = employee.Supervisors.ToList().ConvertAll(c => c.ToEmployeeInfoLite());

            return result;
        }


        public static EmployeeInfoBase ToEmployeeInfoBase(this Employee employee)
        {
            if (null == employee) return null;

            var result = ConvertTo<EmployeeInfoBase>(employee);

            return result;
        }


        static T ConvertTo<T>(Providers.EmployeeInfo.Employee employee) where T : FB.Data.Models.EmployeeInfoBase, new()
        {
            T result = new T
            {
                EMPCode = employee.EMPCode,
                FirstName = employee.FirstName,
                LastName = employee.LastName,
                Alias = employee.Alias,
                ChineseName = employee.ChineseName,
                Email = employee.Email,
                RefCode = employee.RefCode,
                UserID = employee.UserID,
                UserName = employee.UserName,
                Status = employee.Status,
                Gender = employee.Gender,
            };


            if (null != employee.PostDetails && employee.PostDetails.Count() > 0)
            {
                result.PostDetails = (from p in employee.PostDetails
                                      select new FB.Data.Models.PostDetail
                                      {
                                          COMCode = p.Company.COMCode,
                                          COMName = p.Company.Name,
                                          DEPTCode = (null != p.Department) ? p.Department.DEPTCode : string.Empty,
                                          DEPTName = (null != p.Department) ? p.Department.Name : string.Empty,
                                          TEAMCode = (null != p.Team) ? p.Team.TEAMCode : string.Empty,
                                          TEAMName = (null != p.Team) ? p.Team.Name : string.Empty,
                                          Grading = p.Position.Grading,
                                          IsPrimary = p.IsPrimary,
                                          ID = p.ID,
                                          POSCode = p.Position.POSCode,
                                          POSName = p.Position.Name,
                                          Title = p.CropTitle,
                                      }).ToList();


                result.Companies = result.PostDetails.Select(s => new FB.Data.Models.Company { Code = s.COMCode, Name = s.COMName }).Distinct();
                result.Departments = result.PostDetails.Select(s => new FB.Data.Models.Department { Code = s.DEPTCode, Name = s.DEPTName }).Distinct();
                result.Teams = result.PostDetails.Select(s => new FB.Data.Models.Team { Code = s.TEAMCode, Name = s.TEAMName }).Distinct();
                result.Positions = result.PostDetails.Select(s => new FB.Data.Models.Position { Code = s.POSCode, Name = s.POSName, Grading = s.Grading }).Distinct();
            }


            return result;
        }


        

    }



}
